<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Address extends Auth_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->model("address_model","address");
        
    }
 
    public function provinceList(){
        $this->isArrays = true;
        $fields = " id, name ";
        //基本信息
        $provinceList = array();
        $provinceList = $this->address->getProvinceList($fields);
        $this->createApi($provinceList);
        exit();
    }

    public function cityList(){
        $this->isArrays = true;
        $params['id'] = $this->queryData['id'];
        $validate = new Validate([
            'id' => 'require|number',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        $cityList = array();
        $fields = " id, name ";
        //基本信息
        $cityList = $this->address->getCityList($params['id'], $fields);
        
        $this->createApi($cityList);
        exit();
    }


    /**
     * 我的收货地址页面
     *
     */
    public function index(){
        $addressData = array();
        $userInfo = $this->userDetail;

        $addressData = $this->address->address_list($userInfo['AccountID'], " id,provinceid,cityid,area,AccountID,isDefault,mobile,receiver ");
        $this->createApi($addressData);
    }

    /**
     * 变更默认收货地址
     *
     */
    public function setDefault()
    {
        $params['id'] = $this->queryData['id'];
        $validate = new Validate([
            'id' => 'require|number',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        $userInfo = $this->userDetail;

        $result = $this->address->address_set_default($params['id'], $userInfo['AccountID']);
        if(!$result)
        {
            $this->errorCode = 50006;
            $this->createApi();exit;
        }
        $this->createApi();
        exit;
    }

    /**
     * 获取默认收货地址
     *
     */
    public function getDefault()
    {
        $userInfo = $this->userDetail;
        $default_address = array();
        $default_address = $this->address->address_get_default($userInfo['AccountID']," * ");
        $fields = " id, name ,pid ";
        if($default_address){
             if($default_address['proviceid']){;
                $province = $this->address->getRegionById($default_address['proviceid'], $fields);
                $addressData[$key]['proviceName'] = $province['name'];
            }
            if($default_address['cityid']){
                $city = $this->address->getRegionById($default_address['cityid'], $fields);
                $addressData[$key]['cityName'] = $city['name'];
            }
            if($default_address['countyid']){
                $county = $this->address->getRegionById($default_address['countyid'], $fields);
                $addressData[$key]['countyName'] = $county['name'];
            }
        }
        $this->createApi($default_address);
    }


    /**
     * 根据id获取收货地址
     *
     */
    public function detail()
    {
        $params['id'] = 15;
        $validate = new Validate([
            'id' => 'require|number',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        $userInfo = $this->userDetail;
        $list = $this->address->address_detail($params['id']," id,provinceid,cityid,area,isdefault,mobile,receiver",$userInfo['AccountID']);
        $this->createApi($list);
    }

    /**
     * 新增收货地
     *
     */
    public function add()
    {
        $params['receiver'] = $this->queryData['receiver'];
        $params['area'] = $this->queryData['area'];
        $params['mobile'] = $this->queryData['mobile'];
        $params['isDefault'] = $this->queryData['isDefault'];
        $params['provinceid'] = $this->queryData['provinceid'];
        $params['cityid'] = $this->queryData['cityid'];
        
        $validate = new Validate([
            'receiver' => 'require|string',
            'area' => 'require|string',
            'mobile' => 'require|string',
            'isDefault' => 'require|number',
            'provinceid' => 'require|number',
            'cityid' => 'require|number',
            
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        
        // 第一次添加收货地址,直接设置成默认
        $userInfo = $this->userDetail;
        $params['AccountID'] = $userInfo['AccountID'];
        $default_address = $this->address->address_get_default($userInfo['AccountID']," * ");
        if(empty($default_address))
        {
            $params['isDefault'] = 1; // 设为默认
        }
        $addressData = $this->address->address_add($params,$userInfo['AccountID']);
        $this->createApi(array("addressId"=>$addressData));
    }


    /**
     * 编辑收货地址
     *
     */
    public function edit()
    {
        $params['id'] = $this->queryData['id'];
        $params['receiver'] = $this->queryData['receiver'];
        $params['area'] = $this->queryData['area'];
        $params['mobile'] = $this->queryData['mobile'];
        $params['isDefault'] = $this->queryData['isDefault'];
        $params['provinceid'] = $this->queryData['provinceid'];
        $params['cityid'] = $this->queryData['cityid'];

        $validate = new Validate([
            'id' => 'require|number',
            'receiver' => 'require|string',
            'area' => 'require|string',
            'mobile' => 'require|string',
            'isDefault' => 'require|number',
            'provinceid' => 'require|number',
            'cityid' => 'require|number',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        $userInfo = $this->userDetail;
        $addressList = $this->address->address_detail($params['id'],'id',$userInfo['AccountID']);
        if(empty($addressList))
        {
            $this->errorCode = 90002;
            $this->createApi();exit;
        }
        
        $data = array(
            'receiver'=>$params['receiver'],
            'area'=>$params['area'],
            'mobile'=>$params['mobile'],
            'isDefault'=>$params['isDefault'],
            'AccountID'=>$userInfo['AccountID'],
            'provinceid'=>$userInfo['provinceid'],
            'cityid'=>$userInfo['cityid']
            );
        $addressData = $this->address->address_edit($params['id'], $data);
        $this->createApi($addressData);
    }

    /**
     * 删除收货地址
     *
     */
    public function del()
    {
        $params['id'] = $this->queryData['id'];
        $validate = new Validate([
            'id' => 'require|number',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        $userInfo = $this->userDetail;
        $addressList = $this->address->address_detail($params['id'],'id',$userInfo['AccountID']);
        if(empty($addressList))
        {
            $this->errorCode = 90005;
            $this->createApi();exit;
        }
        $addressData = $this->address->address_del($params['id'],$userInfo['AccountID']);
        if(!$addressData)
        {
            $this->errorCode = 50006;
            $this->createApi();exit;
        }
        $this->createApi();
        exit;
    }


}

/* End of file me.php */
/* Location: ./application/controllers/me.php */
